﻿using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Simp.Model.CoreExpressions.Sequences
{
	/// <summary>Encapsulates a function computing the lenght of a sequence.</summary>
	[ExpressionName("Len")]
	[ParameterPolicy("sequence")]
	[DebuggerDisplay("{ExpressionHelper.Serialize(this)}")]
	public class LenExpression : IExpression, ISimplifiableExpression
	{
		#region ISimplifiableExpression Members
		IExpression ISimplifiableExpression.Simplify(SimplificationMode simplificationMode, params IExpression[] parameterList)
		{
			if (parameterList == null)
			{
				throw new ArgumentNullException("parameterList");
			}
			if (parameterList.Length == 1)
			{
				ISequenceAccessor accessor = ExpressionHelper.GetAccessor(parameterList[0]);

				if (accessor != null)
				{
					return new IntegerExpression(accessor.Count);
				}
			}

			return null;
		}
		#endregion
	}
}